package com.kuang.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private  static String url ;
    private static String username ;
    private static String password ;
    private static String driver ;

    static {
        InputStream in= BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties=new Properties();
        try {
            properties.load(in);
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
            driver=properties.getProperty("driver");
            System.out.println("url: "+ url);
            System.out.println("username: "+ username);
            System.out.println("password: "+ password);
            System.out.println("driver: "+ driver);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    获得连接
    public static Connection getConnect()  {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection=DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        }
        return  connection;
    }

//    查询
    public static ResultSet execute(Connection connection ,
                                    String sql,
                                    Object[] param,
                                    ResultSet resultSet,
                                    PreparedStatement preparedStatement){
//         preparedStatement = null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            for(int i=0;i<param.length;i++){
                preparedStatement.setObject(i+1,param[i]);
            }
            resultSet=preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    public static int execute(Connection connection ,
                               String sql,
                               Object[] param,
                               PreparedStatement preparedStatement){
        preparedStatement = null;
        int updateRows=0;
        try {
            preparedStatement=connection.prepareStatement(sql);
            for(int i=0;i<param.length;i++){
                preparedStatement.setObject(i+1,param[i]);
            }
             updateRows=preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return updateRows;

    }

    public static Boolean connectClose(Connection connection,
                                    PreparedStatement preparedStatement,
                                    ResultSet resultSet){
        boolean flag = true;
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException e) {
                flag = false;
                e.printStackTrace();
            }
        }
        if(resultSet!=null){
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException e) {
                flag = false;
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                flag = false;
                e.printStackTrace();
            }
        }
        return flag;
    }

}
